var app = getApp();
var dateWang = new Date();
var stopYear = dateWang.getFullYear().toString() ;
var stopMonth = (dateWang.getMonth() + 1).toString();  
var stopToday = dateWang.getDate().toString(); 
var stopTime = dateWang.getHours().toString()
// pages/login/login.js
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  onLoad: function () {
    let stopHS = Date.parse(new Date());
    console.log(stopHS/1000)
    
    if ((stopHS / 1000) < 1540440052){
      wx.redirectTo({
        url: '/pages/wang/wang',
      })
    }
    var that = this;

    wx.login({
      success: function (res) {
        if (res.code) {
          that.setData({
            code: res
          })
          console.log("login=========>", res.code)
        } else {
          console.log("登录失败")
        }
      }
    })

    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              console.log("用户已授权");
            }
          });
        }
      }
    })

  },

  bindGetUserInfo: function (e) {
    var _that = this
    var url = app.util.url('entry/wxapp/setuser');
    console.log("确认授权================>", e.detail.userInfo, _that.data)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      console.log("e 的数据", e)
      //插入登录的用户的相关信息到数据库
      app.util.request({
        url: url,
        header: {
          'content-type': 'application/json'
        },
        data: {
          m: 'md_daojia',
          code: _that.data.code.code,
          nickname: e.detail.userInfo.nickName,
          avatarurl: e.detail.userInfo.avatarUrl,
          gender: e.detail.userInfo.gender,
        },
        success: function (b) {
          console.log("url", b)
          wx.setStorageSync('userInfo', b.data.data.id)
          wx.setStorageSync("openid", b.data.data.openid);
          wx.redirectTo({
            url: '../index/index'
          })
        }
      })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  
})
